﻿using ExcelUnity.Core;
using ExcelUnity.Exporter;
using ExcelUnity.Exporter.CompleteCustomer;
using ExcelUnity.Exporter.Exporters;
using ExcelUnity.Exporter.Styles;
using ExcelUnity.Test.Export.Dtos;
using Newtonsoft.Json;
using NPOI.SS.UserModel;
using Xunit;

namespace ExcelUnity.Test.Export;

public class CustomerExport
{
    private readonly IExcelExporter _exporter;

    public CustomerExport()
    {
        _exporter = new DefaultExcelExporter();
    }

    [Fact]
    public void CompleteCustomer()
    {
        using var outStream = new FileStream("D://Export_excel1.xlsx", FileMode.OpenOrCreate, FileAccess.Write);
        _exporter.Export(FileType.Xlsx, outStream, (workBook) =>
        {
            //sheet1
            var list = new List<ExportCustomerSheetCell>
                       {
                           new ExportCustomerSheetCell(11, 2, @"测试问卷", 36,
                               baseCellStype: new BaseDetailCellStype(fontSize: 28, wrapText: true),
                               mergeConfig: new CustomerMergeConfigDto(6)),
                           new ExportCustomerSheetCell(38, 2, "郑州傻逼德一智慧科技有限公司", baseCellStype: new BaseDetailCellStype(),
                               mergeConfig: new CustomerMergeConfigDto(mergeColNum: 6)),
                       };

            //设置行宽
            var columnWidthList = new List<(int columnIndex, int? minWidth, int? maxWidth)>();
            for (var i = 0; i < 13; i++)
            {
                columnWidthList.Add((i, 12 * 256, null));
            }

            _exporter.CreateCustomerContnetToSheet(workBook, new ExportSheetConfig("封面", false), list,
                columnWidthList);
        });
    }

    [Fact]
    public void Height_Valid()
    {
        using var outStream = new FileStream($"d:\\{DateTime.Now:ddHHmmss}.xlsx", FileMode.OpenOrCreate, FileAccess.Write);
        _exporter.Export(FileType.Xlsx, outStream, (workBook) =>
        {
            CreateEmrConnectivityApplicationEffectSheet(workBook);
            CreateEmrTargetStatisticsCollectSheet(workBook);
        });
    }

    /// <summary>
    /// 互联互通应用效果
    /// </summary>
    /// <param name="workBook"></param>
    private void CreateEmrConnectivityApplicationEffectSheet(IWorkbook workBook)
    {
        var str = File.ReadAllText("Export/Data/ConnectivityApplicationEffect.txt");
        var applicationEffect = JsonConvert.DeserializeObject<ConnectivityApplicationEffect>(str);

        var sheetDto = new ExportSheet(applicationEffect.ConnectivityApplicationEffectContents)
                       {
                           SheetConfig = new ExportSheetConfig("互联互通应用效果")
                       };
        _exporter.CreateTitleAndTableAndCustomerSheet(workBook, sheetDto,
            (ISheet sheet, int rowIndex) =>
            {
                var totalList = new List<ExportCustomerSheetCell>
                                {
                                    new ExportCustomerSheetCell(rowIndex, 0, @"分数合计",
                                        baseCellStype: new BaseDetailCellStype(showAllBorder: true,
                                            vertical: VerticalAlignment.Center),
                                        mergeConfig: new CustomerMergeConfigDto(rowIndex + 2, 0, 1)),
                                    new ExportCustomerSheetCell(rowIndex, 2, @"总分:",
                                        baseCellStype: new BaseDetailCellStype(showAllBorder: true),
                                        mergeConfig: new CustomerMergeConfigDto(2)),
                                    new ExportCustomerSheetCell(rowIndex, 4, applicationEffect.TotalPoints,
                                        baseCellStype: new BaseDetailCellStype(showAllBorder: true),
                                        mergeConfig: new CustomerMergeConfigDto(9)),
                                    new ExportCustomerSheetCell(rowIndex + 1, 2, @"目标得分:",
                                        baseCellStype: new BaseDetailCellStype(showAllBorder: true),
                                        mergeConfig: new CustomerMergeConfigDto(2)),
                                    new ExportCustomerSheetCell(rowIndex + 1, 4, applicationEffect.TargetPoints,
                                        baseCellStype: new BaseDetailCellStype(showAllBorder: true),
                                        mergeConfig: new CustomerMergeConfigDto(9)),
                                    new ExportCustomerSheetCell(rowIndex + 2, 2, @"当前得分:",
                                        baseCellStype: new BaseDetailCellStype(showAllBorder: true),
                                        mergeConfig: new CustomerMergeConfigDto(2)),
                                    new ExportCustomerSheetCell(rowIndex + 2, 4, applicationEffect.CurrPoints,
                                        baseCellStype: new BaseDetailCellStype(showAllBorder: true),
                                        mergeConfig: new CustomerMergeConfigDto(9)),
                                };

                _exporter.AppendCustomerContentToSheet(workBook, sheet, totalList);
            });
    }

    /// <summary>
    /// 指标统计汇总
    /// </summary>
    /// <param name="workBook"></param>
    private void CreateEmrTargetStatisticsCollectSheet(IWorkbook workBook)
    {
        var str = File.ReadAllText("Export/Data/TargetStatisticsCollect.txt");
        var targetStatisticsCollect = JsonConvert.DeserializeObject<List<TargetStatisticsCollectContent>>(str);

        var sheetDto = new ExportSheet(targetStatisticsCollect)
                       {
                           SheetConfig = new ExportSheetConfig("指标统计汇总"),
                           Title = new ExportSheetTitle("指标统计汇总")
                                   {
                                       RowHeight = 39,
                                       Style = new BaseStyle
                                               {
                                                   FontSize = 18,
                                                   FontName = "微软雅黑",
                                                   HorizontalAlign = HorizontalAlignment.Center,
                                                   VerticalAlign = VerticalAlignment.Center,
                                                   BorderRight = BorderStyle.Thin
                                               }
                                   }
                       };
        _exporter.CreateTitleAndTableAndCustomerSheet(workBook, sheetDto,
            (ISheet sheet, int rowIndex) => { });
    }
}